#include"server.hpp"
#include<sstream>
#include<memory>
#include<vector>
const std::string webroot="./wwwroot";
const std::string SEP="\r\n";
class HttpRequest
{
public:
    HttpRequest()
    :_path(webroot)
    {}
public:
    std::string _method;//请求类型
    std::string _url;//请求路径
    std::string _version;//http版本
    std::string _path;
};
std::string HandlerRequest(std::string request)
{
    //读取请求,对request进行处理
    std::cout<<request;
    std::string line;
    size_t pos=request.find(SEP);
    line=request.substr(0,pos);
    std::stringstream ss(line);
    HttpRequest hr;
    ss>>hr._method>>hr._url>>hr._version;
    std::string body;
    if(hr._url=="/hello")
    {
        body="<html><body> hello world</body></html>";
    }
    std::string response;
    response="HTTP/1.0 200 OK"+SEP;
    response+="Content-Length:"+std::to_string(body.size())+SEP;
    response+="Content-Type:text/html"+SEP;
    response+=SEP;
    response+=body;
    std::cout<<response;
    return response;

}
int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        exit(USAGE_ERR);
        std::cout<<"请输入port\n";
    }
    uint16_t port=atoi(argv[1]);
    //std::cout<<port<<std::endl;
    std::unique_ptr<Http::HttpServer> tsvr(new Http::HttpServer(HandlerRequest,port));
    tsvr->InitServer();
    tsvr->Start();
    return 0;
}